import axios from "axios";
import { Loading } from "@/utils/common";
import { message } from "ant-design-vue";
import store from "@/store";

const env = process.env.NODE_ENV;
const baseURL =
   process.env.VUE_APP_BASE_URL;
// console.log(process.env.VUE_APP_BASE_URL,"++++++++++++++");
const service = axios.create({
  baseURL: "https://www.fastmock.site/mock/7dbffa759975376a6a905c4c7658ec9f/mock", // process.env.VUE_APP_BASE_URL
  timeout: 60000, // request timeout
  headers: {
    "Content-Type": "application/json",
  },
});
service.interceptors.request.use((config) => {
  
    const token = store.getters.token;
    if (token) {
      config.headers["authToken"] = token;
    }

  // 上传文件时修改["Content-Type"] = "multipart/form-data"
  if (config.params && config.params.crm_service_http_type == "upload") {
    config.method = "post";
    config.headers["Content-Type"] = "multipart/form-data";
  }
  // 下载文件时修改["Content-Type"]置空
  if (config.params && config.params.crm_service_http_type == "download") {
    config.method = "post";
    config.headers["Content-Type"] = "";
  }
//   if (sessionid) {
//     config.headers["sessionId"] = sessionid;
//   }
  return config;
});
service.interceptors.response.use(
  (response) => {
    // 检查 body code
    if (response.status !== 200) {
      Loading().remove();
      return Promise.reject(new Error(response.message || "Error"));
    }
    // 取出 body
    const body = response.data;
    // 返回 body
    return body;
  },
  // 异常处理
  (error) => {
    Loading().remove();
    message.destroy();
    if (error.response) {
      const data = error.response;
      //尚未登录
      if ([480, 481, 482, 483, 484, 485].indexOf(data.status) > -1) {
        message.warning("登录信息不匹配，请重新登录！");
        store.commit("userInfoStore/updateUserInfo", null); // FIXME 未清除
        // loginOut();
      } else if (data.status == 486) {
        message.warning("无权操作！");
      } else {
        message.error("系统错误，请稍候再试！");
      }
    } else {
      if (error.message == "操作太频繁，请稍后再试") return;
      message.error("系统错误，请稍候再试！");
    }
    return Promise.reject(error);
  }
);
export default service;
